package com.luo.gmall.pms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.PageParamVo;

import com.luo.gmall.pms.mapper.CategoryMapper;
import com.luo.gmall.pms.entity.CategoryEntity;
import com.luo.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryCategoriesByParentId(Long parentId) {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        if (parentId != -1) {
            queryWrapper.eq("parent_id", parentId);
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<CategoryEntity> queryLevelTwoAndThreeCategoryListByParentId(Long parentId) {

        return categoryMapper.queryListCategoryByParentId(parentId);
    }

    @Override
    public List<CategoryEntity> queryLevelOneTwoThreeByLevelThreeId(Long id) {
        // 查询三级分类的信息
        CategoryEntity threeCategoryEntity = baseMapper.selectById(id);

        // 查询二级分类的信息
        CategoryEntity twoCategoryEntity = baseMapper.selectById(threeCategoryEntity.getParentId());

        // 查询一级分类的信息
        CategoryEntity oneCategoryEntity = baseMapper.selectById(twoCategoryEntity.getParentId());

        return Arrays.asList(oneCategoryEntity, twoCategoryEntity, threeCategoryEntity);
    }

}